﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// quanzi 的摘要说明
    /// </summary>
    public class quanzi : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int totalCountt = 0;
        public void ProcessRequest(HttpContext context)
        {
            int quanzi_id = DTRequest.GetQueryInt("quanzi_id", 1);
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"company\":\"日照创诚\",");
            strTxt.Append("\"scq\":\"" + quanzi_id + "\",");
            //获取求购信息
            BLL.qiugou qg_bll = new BLL.qiugou();
            DataSet qg_ds = qg_bll.GetList(10, "status=1 and quanzi_id=" + quanzi_id, "id desc");
            //如果记录存在
            if (qg_ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"qiugou\":[");
                for (int i = 0; i < qg_ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = qg_ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr["size_long"] + "×" + dr["size_wide"] + "×" + dr["size_height"] + "\"");
                    if (string.IsNullOrEmpty(dr["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string picurl = dr["img_url"].ToString();
                        if (picurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                        }
                    }
                    strTxt.Append(",\"content\":\"" + Microsoft.JScript.GlobalObject.escape(dr["content"].ToString().Replace("<p>", "").Replace("</p>", "")) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < qg_ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取供应信息
            BLL.gongying bll5 = new BLL.gongying();
            DataSet ds5 = bll5.GetList(10, "status=1 and quanzi_id=" + quanzi_id, "id desc");
            //如果记录存在
            if (ds5.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"gongying\":[");
                for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
                {
                    DataRow dr5 = ds5.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr5["id"]);
                    strTxt.Append(",\"title\":\"" + dr5["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr5["size_long"] + "×" + dr5["size_wide"] + "×" + dr5["size_height"] + "\"");
                    if (string.IsNullOrEmpty(dr5["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string picurl = dr5["img_url"].ToString();
                        if (picurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                        }
                    }
                    strTxt.Append(",\"content\":\"" + Microsoft.JScript.GlobalObject.escape(dr5["content"].ToString().Replace("<p>", "").Replace("</p>", "")) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr5["add_time"])) + "\"");
                    strTxt.Append(",\"market_price\":\"" + dr5["price"] + "/" + dr5["unit"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr5["mobile"] + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr5["lianxiren"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds5.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //推荐企业
            BLL.index_company bll2 = new BLL.index_company();
            DataSet ds2 = bll2.GetList(6, "quanzi_id=" + quanzi_id, "sort_id asc,id desc");
            //如果记录存在
            if (ds2.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"index_company\":[");
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    DataRow dr2 = ds2.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"company_id\":" + dr2["company_id"]);
                    strTxt.Append(",\"title\":\"" + dr2["title"] + "\"");
                    string picurl = dr2["img_url"].ToString();
                    if (picurl.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"img_url\":\"" + picurl + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                    }
                    string _mobile = new BLL.company().GetMobile(Convert.ToInt32(dr2["company_id"]));
                    strTxt.Append(",\"mobile\":\"" + _mobile + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds2.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"index_company\":[],");
            }
            //获取人力资源列表
            BLL.jobs bll = new BLL.jobs();
            DataSet ds = bll.GetList(10, "status=1 and quanzi_id=" + quanzi_id, "id desc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"job_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"type_id\":" + dr["type_id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"job_list\":[],");
            }
            //物流信息列表
            BLL.wuliu wl_bll = new BLL.wuliu();
            DataSet wl_ds = wl_bll.GetList(10, "status=1 and quanzi_id=" + quanzi_id, "id desc");
            //如果记录存在
            if (wl_ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"wuliu_list\":[");
                for (int i = 0; i < wl_ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = wl_ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"type_id\":" + dr["type_id"]);
                    if (Convert.ToInt32(dr["type_id"]) == 1)
                    {
                        strTxt.Append(",\"type_name\":\"求车\"");
                    }
                    else
                    {
                        strTxt.Append(",\"type_name\":\"求货\"");
                    }
                    strTxt.Append(",\"title\":\"" + dr["start_address"] + "->" + dr["end_address"] + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    if (Convert.ToInt32(dr["user_id"]) > 0)
                    {
                        string mobile = new BLL.users().GetUserName(Convert.ToInt32(dr["user_id"]));
                        strTxt.Append(",\"mobile\":\"" + mobile + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < wl_ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"wuliu_list\":[],");
            }
            //获取圈子新鲜事
            BLL.xinxianshi xbll = new BLL.xinxianshi();
            DataSet xds = xbll.GetList(10, 1, "quanzi_id=" + quanzi_id, "id desc", out this.totalCount);
            int total_page = this.totalCount / 10;
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (xds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"xinxianshi_list\":[");
                for (int i = 0; i < xds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = xds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr["remark"]) + "\"");
                    strTxt.Append(",\"user_id\":" + dr["user_id"]);
                    if (Convert.ToInt32(dr["user_id"]) > 0)
                    {
                        string _username = new BLL.users().GetUserName(Convert.ToInt32(dr["user_id"]));
                        string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr["user_id"]));
                        strTxt.Append(",\"user_name\":\"" + _username + "\"");
                        if (string.IsNullOrEmpty(_avatar))
                        {
                            strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                        }
                        else
                        {
                            if (_avatar.Contains("shicaiquanzi"))
                            {
                                strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                            }
                            else
                            {
                                strTxt.Append(",\"avatar\":\"http://www.shicaiquanzi.com" + _avatar + "\"");
                            }
                        }
                    }
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append(",\"hits\":" + dr["hits"]);
                    
                    
                    BLL.xxs_albums pbll = new BLL.xxs_albums();
                    DataSet pds = pbll.GetLists(int.Parse(dr["id"].ToString()));
                    if (pds.Tables[0].Rows.Count > 0)
                    {
                        strTxt.Append(",\"pic_list\":[");
                        for (int ii = 0; ii < pds.Tables[0].Rows.Count; ii++)
                        {
                            DataRow pdr = pds.Tables[0].Rows[ii];
                            string picurl = pdr["thumb_path"].ToString();
                            if (picurl.Contains("shicaiquanzi.com"))
                            {
                                strTxt.Append("{\"picurl\":\"http://" + picurl + "\"}");
                            }
                            else
                            {
                                strTxt.Append("{\"picurl\":\"http://www.shicaiquanzi.com" + pdr["thumb_path"] + "\"}");
                            }
                            //是否加逗号
                            if (ii < pds.Tables[0].Rows.Count - 1)
                            {
                                strTxt.Append(",");
                            }
                        }
                        strTxt.Append("]");
                    }
                    //调用赞列表
                    BLL.zan zbll = new BLL.zan();
                    DataSet zds = zbll.GetList(0, "article_id=" + int.Parse(dr["id"].ToString()), "id desc");
                    if (zds.Tables[0].Rows.Count > 0)
                    {
                        string zan_info = string.Empty;
                        for (int z = 0; z < zds.Tables[0].Rows.Count; z++)
                        {
                            DataRow zdr = zds.Tables[0].Rows[z];
                            int _userid = int.Parse(zdr["user_id"].ToString());
                            string _uname = new BLL.users().GetUserName(_userid);
                            zan_info += _uname;
                            //是否加逗号
                            if (z < zds.Tables[0].Rows.Count - 1)
                            {
                                zan_info += ",";
                            }
                        }
                        strTxt.Append(",\"zan_info\":\"" + Microsoft.JScript.GlobalObject.escape(zan_info) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"zan_info\":\"\"");
                    }
                    //获取评论列表
                    BLL.comment cbll = new BLL.comment();
                    DataSet cds = cbll.GetList(0, "article_id=" + int.Parse(dr["id"].ToString()), "id desc");
                    if (cds.Tables[0].Rows.Count > 0)
                    {
                        string c_info = string.Empty;
                        for (int c = 0; c < cds.Tables[0].Rows.Count; c++)
                        {
                            DataRow cdr = cds.Tables[0].Rows[c];
                            int _userid = int.Parse(cdr["user_id"].ToString());
                            string _uname = new BLL.users().GetUserName(_userid);
                            c_info += "<p><strong>" + _uname + "：</strong>" + cdr["remark"] + "</p>";
                        }
                        strTxt.Append(",\"common_info\":\"" + Microsoft.JScript.GlobalObject.escape(c_info) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"common_info\":\"\"");
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < xds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"xinxianshi_list\":[]");
            }

            //获取信息
            BLL.xinxi xxbll = new BLL.xinxi();
            string strWhere = "id>0";
            if (quanzi_id > 0)
            {
                strWhere += " and quanzi_id=" + quanzi_id;
            }
            DataSet xxds = xxbll.GetList(10, 1, "quanzi_id=" + quanzi_id, "add_time desc", out this.totalCount);
            int xx_total_page = this.totalCount / 10;
            strTxt.Append(",\"xx_total_page\":" + xx_total_page.ToString());
            //如果记录存在
            if (xxds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append(",\"xinxi_list\":[");
                for (int i = 0; i < xxds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr3 = xxds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr3["id"]);
                    strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr3["content"].ToString()) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr3["add_time"])) + "\"");
                    strTxt.Append(",\"hits\":" + dr3["hits"]);
                    strTxt.Append(",\"user_id\":" + dr3["user_id"]);
                    if (Convert.ToInt32(dr3["user_id"]) > 0)
                    {
                        string _username = new BLL.users().GetUserName(Convert.ToInt32(dr3["user_id"]));
                        string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr3["user_id"]));
                        strTxt.Append(",\"user_name\":\"" + _username + "\"");
                        if (string.IsNullOrEmpty(_avatar))
                        {
                            strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                        }
                        else
                        {
                            if (_avatar.Contains("shicaiquanzi"))
                            {
                                strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                            }
                            else
                            {
                                strTxt.Append(",\"avatar\":\"http://www.shicaiquanzi.com" + _avatar + "\"");
                            }
                        }
                    }
                    BLL.xinxi_albums pbll = new BLL.xinxi_albums();
                    DataSet pds = pbll.GetLists(int.Parse(dr3["id"].ToString()));
                    if (pds.Tables[0].Rows.Count > 0)
                    {
                        strTxt.Append(",\"pic_list\":[");
                        for (int ii = 0; ii < pds.Tables[0].Rows.Count; ii++)
                        {
                            DataRow pdr = pds.Tables[0].Rows[ii];
                            string picurl = pdr["thumb_path"].ToString();
                            if (picurl.Contains("shicaiquanzi.com"))
                            {
                                strTxt.Append("{\"picurl\":\"http://" + picurl + "\"}");
                            }
                            else
                            {
                                strTxt.Append("{\"picurl\":\"http://www.shicaiquanzi.com" + pdr["thumb_path"] + "\"}");
                            }
                            //是否加逗号
                            if (ii < pds.Tables[0].Rows.Count - 1)
                            {
                                strTxt.Append(",");
                            }
                        }
                        strTxt.Append("]");
                    }
                    else
                    {
                        strTxt.Append(",\"pic_list\":[]");
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < xxds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"status\":2,\"xinxi_list\":[]");
            }

            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}